<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class model_trangchu extends CI_Model
{
    public function __construct() {
        parent::__construct();
    }

    public function monhoc($lop) {

    	$this->db->select('tenmonhoc');
        $this->db->from('monhoc');
        $this->db->where('khoi', $lop);
        $this->db->where('tinhtrang', 1);

      	$query = $this->db->get();
        return $query->result();
    }

    public function thongbao() {
        $this->db->select('mathongbao, tieude, ngaydang');
        $this->db->from('thongbao');
        $this->db->where('tinhtrang', 1);
        $this->db->order_by('ngaydang', 'desc');

        $query = $this->db->get();
        return $query->result();
    }

    public function noidungthongbao($mathongbao) {
        $this->db->select('tieude, noidung, ngaydang, tennguoidang, filedinhkem');
        $this->db->from('thongbao');
        $this->db->where('mathongbao', $mathongbao);
        $this->db->where('tinhtrang', 1);

        $query = $this->db->get();
        if($query->num_rows > 0) {
            return $query->result();
        } else {
            return null;
        }
        
    }

    public function baigiang() {
        $this->db->select('mabaigiang, tenbaigiang, baigiang.magiaovien, hoten, thoigianupload, thoigiancapnhat');
        $this->db->from('baigiang');
        $this->db->join('giaovien', 'baigiang.magiaovien=giaovien.magiaovien');
        $this->db->where('baigiang.tinhtrang', 1);
        $this->db->limit(4);

        $query = $this->db->get();
        if($query->num_rows > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    public function noidungbaigiang($mabaigiang) {
        $this->db->select('tenbaigiang, url, baigiang.magiaovien, hoten, thoigianupload, thoigiancapnhat');
        $this->db->from('baigiang');
        $this->db->join('giaovien', 'baigiang.magiaovien=giaovien.magiaovien');
        $this->db->where('mabaigiang', $mabaigiang);
        $this->db->where('baigiang.tinhtrang', 1);

        $query = $this->db->get();
        if($query->num_rows > 0) {
            $query = $query->result();
            return $query[0];
        } else {
            return null;
        }
    }

    public function baitaplienquan($mabaigiang) {
        $this->db->select('mabaitap, tenbaitap');
        $this->db->from('baitap');
        $this->db->where('mabaigiang', $mabaigiang);
        $this->db->where('tinhtrang', 1);

        $query = $this->db->get();
        if($query->num_rows > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    public function baitap() {
        $this->db->select('mabaitap, tenbaitap, mota, baitap.mabaigiang, tenbaigiang');
        $this->db->from('baitap');
        $this->db->join('baigiang', 'baitap.mabaigiang=baigiang.mabaigiang');
        $this->db->where('baitap.tinhtrang', 1);
        $this->db->limit(4);

        $query = $this->db->get();
        if($query->num_rows > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    public function noidungbaitap($mabaitap) {
        $this->db->select('tenbaitap, cau, loaibaitap, noidung, a, b, c, d, url');
        $this->db->from('chitietbaitap');
        $this->db->join('baitap', 'chitietbaitap.mabaitap=baitap.mabaitap');
        $this->db->where('tinhtrang', 1);
        $this->db->where('chitietbaitap.mabaitap', $mabaitap);

        $query = $this->db->get();
        if($query->num_rows > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    public function CountCauHoi($mabt) {
        $this->db->select('cau');
        $this->db->from('chitietbaitap');
        $this->db->where('mabaitap', $mabt);
        return $this->db->count_all_results();
    }

    public function GetDapAn($mabt) {
        $this->db->select('mabaitap, cau, dapan');
        $this->db->from('chitietbaitap');
        $this->db->where('mabaitap', $mabt);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    public function InsertLamBai($data) {
        $this->db->insert('lambai', $data);
    }

    public function GetMaLamBai($mahs, $mabt) {
        $this->db->select('malambai, diem, thoigianlam, giaoviennhanxet');
        $this->db->from('lambai');
        $this->db->where('mahocsinh', $mahs);
        $this->db->where('mabaitap', $mabt);
        $this->db->order_by('malambai', 'desc');
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            $query = $query->result();
            return $query[0];
        } else {
            return null;
        }
    }

    public function InsertChiTietLamBai($data) {
        $this->db->insert_batch('chitietlambai', $data);
    }

    public function GetBaiTap($mabt) {
        $this->db->select('mabaitap, tenbaitap, baitap.mota, baitap.mabaigiang, tenbaigiang, tenmonhoc, khoi');
        $this->db->from('baitap');
        $this->db->join('baigiang', 'baitap.mabaigiang=baigiang.mabaigiang');
        $this->db->join('monhoc', 'baitap.mamonhoc=monhoc.mamonhoc');
        $this->db->where('baitap.tinhtrang', 1);
        $this->db->where('mabaitap', $mabt);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            $query = $query->result();
            return $query[0];
        } else {
            return null;
        }
    }

    public function laybaitap() {
        $this->db->select('mabaitap, tenbaitap, baitap.mabaigiang, tenbaigiang, tenmonhoc, khoi');
        $this->db->from('baitap');
        $this->db->join('baigiang', 'baitap.mabaigiang=baigiang.mabaigiang');
        $this->db->join('monhoc', 'baitap.mamonhoc=monhoc.mamonhoc');
        $this->db->where('baitap.tinhtrang', 1);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    public function noidungbailam($malb) {
        $this->db->select('tenbaitap, chitietbaitap.cau, loaibaitap, noidung, chitietbaitap.dapan, a, b, c, d, url, traloi');
        $this->db->from('chitietbaitap');
        $this->db->join('baitap', 'chitietbaitap.mabaitap=baitap.mabaitap');
        $this->db->join('lambai', 'baitap.mabaitap=lambai.mabaitap');
        $this->db->join('chitietlambai', 'lambai.malambai=chitietlambai.malambai');
        $this->db->where('chitietlambai.malambai', $malb);
        $this->db->where('chitietlambai.cau=chitietbaitap.cau', null, false);

        $query = $this->db->get();
        if($query->num_rows > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    public function GetLop($lop) {
        $this->db->select('malop, tenlop, gvcn, hoten, namhoc');
        $this->db->from('lop');
        $this->db->join('giaovien', 'lop.gvcn=giaovien.magiaovien');
        $this->db->like('tenlop', $lop);
        $this->db->where('lop.tinhtrang', 1);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return null;
        }
    }
}